<template>
	<view class="container">
		<view class="search-bar ">
			<UniSearchBar :placeholder="`${$t('storedetail.search.placeholder')}`" v-model="keyword" @confirm="search"
				:foucs="numFocus"
				@blur="formblur"
				@input="forminput"
				@cancel="cancel" cancel-text="cancel">
			</UniSearchBar>
		</view>
		<view class="list">
			<uni-list>
				<uni-list-item v-for="(item, index) in table" :key="index" showArrow 
				:title="`${$t('storedetail.item.name')} ：${item.drugName}  
				${$t('storedetail.item.batchNumber')} ：${item.batchNumber}
				`" 
				:note="`${$t('storedetail.item.totalNum')}:${item.num} `"
				:rightText="`${$t('storedetail.item.warehouse')}:${item.warehouseName} \b
				 ${$t('storedetail.item.shelfName')}:${item.shelfName}`" link="navigateTo"
					:to="`/pages/store/store?id=${item.id}`" />
			</uni-list>
			<uni-load-more :status="status" :icon-size="14" :content-text="contentText" @clickLoadMore="loadmore" />
		</view>
		<view>
			<keyboard-listener @keydown="scancodelisten"></keyboard-listener>
		</view>
	</view>
</template>

<script>
import api from '../../common/api.js'
import common from '../../common/common.js'
import KeyboardListener from 'uniapp-keyboard-listener'
import UniSearchBar from '@/components/uni-search-bar/uni-search-bar.vue'
export default {
	components: {
		UniSearchBar,
		KeyboardListener
	},
	data() {
		return {
			status: 'more',
			contentText: {
				contentdown: this.$t('load.more.contentdown'),     //'查看更多',
				contentrefresh: this.$t('load.more.contentrefresh'),  //'加载中',
				contentnomore: this.$t('load.more.contentnomore')   // '没有更多了'
			},
			id: '',
			keyword: '', //批次号
			table: [],
			pageIndex: 1,
			code:'',
			numFocus:false,
			pdacode:''
		}
	},
	async onLoad(option) {
		this.id = option.id
		console.log('上一个页面传递过来的参数', option)
		console.log('id', option.id)
		if (this.id !== undefined) {
			await this.findbyId()
		} else {
			uni.navigateBack()
		}
	},
	async onUnload(){
		
	},
	onNavigationBarButtonTap(e) {
		console.log(e, '----e---')
		let that = this
		uni.scanCode({
			success: async function (res) {
				console.log('条码类型：' + res.scanType);
				console.log('条码内容：' + res.result);
				that.code = res.result
				await that.findByCode()
			}
		});
	},
	
	methods: {
		forminput(){
			this.pdacode = ''
		},
		formfocus() {
			this.pdacode = ''
			setTimeout(() => {
				this.isFocus = false
				this.numFocus = true
			}, 0)
		},
		formblur() {
			// console.log(e.detail.value)
			this.pdacode = ''
			this.numFocus = false;
		},
		async scancodelisten(e) {
			let keyCode = e.keyCode
			// console.log(e, '-----e---')
			if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122)) {
				console.log('字母输入:', e.key);
				this.pdacode = this.pdacode + e.key
			} else if (keyCode >= 48 && keyCode <= 57) {
				console.log('数字输入:', e.key);
				console.log(this.numFocus, 'numFocus');
				this.pdacode = this.pdacode + e.key
			} else if (keyCode === 13) {
				this.code = this.pdacode
				if(this.code!==''){
					await this.findByCode()
				}
				this.pdacode = ''
			} else {
				console.log('其他输入:', e.key);
			}
		
			console.log(this.pdacode, '-----this.pdacode---')
		},
		async findByCode(){
			uni.showLoading()
			let ret = await common.scanInboundCode(this.code)
			if(ret.code===0 && ret.data){
				if(ret.data.inboundOrderId === this.id){
				uni.navigateTo({
					url:`/pages/store/store?id=${this.code}`
				})
				}else{
					this.$nextTick(()=>{
						uni.showToast({
							title: this.$t('scanfun.fail'),
							icon: 'none'
						});
					})
				}
			}else{
				uni.hideLoading()
				uni.showToast({
					title: ret.message,
					icon: 'none'
				});
			}
			
		},
		async onShow(option) {
			this.pageIndex = 1
		},
		async findbyId() {
			let ret = await api.getInboundDetailPage({
				inboundNo: "",
				page: { pageNumber: 1, pageSize: 50 },
				inboundOrderId: this.id,
			})
			console.log('findbyId-----findbyId',ret)
			if (ret.code === 0) {
				this.table = ret.data.items
			}
		},
		async cancel() {
			this.pageIndex = 1
			this.keyword = ''
			await this.findbyId();
		},
		async search(e) {
			this.pageIndex = 1
			this.keyword = e.value
			let ret = await api.getInboundDetailPage({
				keyword: this.keyword,
				page: { pageNumber: 1, pageSize: 50 },
				inboundOrderId: this.id,
			})
			if (ret.code === 0) {
				this.table = ret.data.items
			} else {
				this.table = []
			}
		},
		async loadmore() {
			this.pageIndex++
			let ret = await api.getInboundDetailPage({
				keyword: this.keyword,
				page: { pageNumber: this.pageIndex, pageSize: 50 },
				inboundOrderId: this.id,
			})
			if (ret.code === 0) {
				if (ret.data.items.length > 0) {
					this.table = [].concat(this.table, ret.data.items)
				} else {
					this.pageIndex--
				}
			} else {
				this.pageIndex--
			}
		}
	}
}
</script>

<style>
.container {
	height: 100vh;
}

.search-bar {
	padding: 10px;
	/* 根据实际高度调整，示例假设搜索栏高度约50px */
	flex-shrink: 0;
	margin-left: 10rpx;
	margin-right: 20rpx;
}

.title-item {

	padding-bottom: 10rpx;
	padding-top: 10rpx;
}
</style>
